package com.example.himalaya;

import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager;
import androidx.viewpager.widget.ViewPager;

import com.example.himalaya.adapters.IndicatorAdapter;
import com.example.himalaya.adapters.MainContentAdapter;
import com.example.himalaya.utils.LogUtils;
import com.google.gson.Gson;
import com.ximalaya.ting.android.opensdk.datatrasfer.CommonRequest;
import com.ximalaya.ting.android.opensdk.datatrasfer.IDataCallBack;
import com.ximalaya.ting.android.opensdk.model.category.Category;
import com.ximalaya.ting.android.opensdk.model.category.CategoryList;

import net.lucode.hackware.magicindicator.MagicIndicator;
import net.lucode.hackware.magicindicator.ViewPagerHelper;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator;

import org.xutils.common.util.LogUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MainActivity extends FragmentActivity {

    private static final String TAG = "MainActivity";
    private MagicIndicator mMagicIndicator;
    private ViewPager mViewPager;
    private IndicatorAdapter mIndicatorAdapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initview();
        initEvent();

    }

    private void initview() {
        mMagicIndicator =this.findViewById(R.id.magic_indicator7);
        mMagicIndicator.setBackgroundColor( this.getResources().getColor(R.color.mainColor));
        // 创建Indicator的适配器
        mIndicatorAdapter =new IndicatorAdapter(this);
        CommonNavigator commonNavigator =new CommonNavigator(this);
        commonNavigator.setAdjustMode(true);//自我调节位置
        commonNavigator.setScrollPivotX(0.65f);
        commonNavigator.setAdapter(mIndicatorAdapter);
        //ViewPager
        mViewPager =this.findViewById(R.id.contentpage);
        //创建内容适配器
        FragmentManager fragmentManager= getSupportFragmentManager();
        MainContentAdapter mainContentAdapter=new MainContentAdapter(fragmentManager);
        mViewPager.setAdapter(mainContentAdapter);

        //绑定ViewPager与indicator绑定到一起
        mMagicIndicator.setNavigator(commonNavigator);
        ViewPagerHelper.bind(mMagicIndicator,mViewPager);
    }
    private void initEvent(){
        mIndicatorAdapter.setOnIndicatorTapClickListener(new IndicatorAdapter.OnIndicatorTapClickListener() {
            @Override
            public void onTabClick(int index) {
                LogUtils.d(TAG,"click index "+ index);
                if(mViewPager!=null){
                     mViewPager.setCurrentItem(index);
                }
            }
        });

    }

}
